# -*- coding: utf-8 -*-
# -------------------------------
# @PyCharm：PyCharm 2023.1.3 (Community Edition)
# @Python：Python 3.8.10
# @时间：2024/11/21 21:05
# -------------------------------

import json
import random

from setting import filter_standard
from db import DB

def generate_data():  # 模拟设备生成运行数据
    dic = {
        'ranshaodaiwen': random.randint(*(1700, 2086)),  # 燃烧带温
        'rongrongdaiwen': random.randint(*(1350, 2000)), # 熔融带温
        'shangbuwen': random.randint(*(200, 300)),       # 上部温
        'xiabuwen': random.randint(*(300, 1350)),        # 下部温
        'lengfengya': random.uniform(*(0.4, 0.80)),      # 冷风压
        'refengya': random.uniform(*(0, 0.40)),          # 热风压
        'ludingya': random.uniform(*(0, 0.789994))       # 炉顶压
    }
    return json.dumps(dic)

def filter_data(dic):  # 异常数据数据过滤
    ctime = dic.pop("ctime")
    for key, value in dic.items():
        for i in range(2,-1,-1):
            if not(value > filter_standard[key]["max"][i] or value < filter_standard[key]["min"][i]):
                continue
            new_dic = {"error_item": key,"error_value": value,"extent":i + 1,"ctime":ctime}
            yield new_dic
            break

if __name__ == '__main__':
    dic = {'ranshaodaiwen': 1730, 'rongrongdaiwen': 1950, 'shangbuwen': 292, 'xiabuwen': 1144, 'lengfengya': 0.6951462524569727, 'refengya': 0.13937259898405538, 'ludingya': 0.6075421325060224, 'ctime': '2024-11-25 13:36:55'}
    db_project = DB()
    for i in filter_data(dic):
        db_project.db_write(i, "alarm_data")
